#include "box.h"
#include <stdio.h>

void parseBoxes(box_t *boxList){
    FILE *file;
    file = fopen("data/boxes.dta", "r");
    int qty = 1;
    if(file){
        char line [ 128 ];
        char seps[]   = ",\n";
        while (fgets(line, sizeof line, file)!= NULL){
            char *config = strtok(line, seps);
            box_t *box = (box_t *)malloc(sizeof(box_t));
            if(!box){
                kill("Error de memoria.");
            }
            box->name = malloc(strlen(config)+1);
            strcpy(box->name, config);
            config = strtok(NULL, seps);
            sscanf(config,"%d", &box->attack);
            config = strtok(NULL, seps);
            sscanf(config,"%d", &box->defense);
            config = strtok(NULL, seps);

            boxList[qty-1] = *box;
            qty++;
            boxList = (box_t *)realloc(boxList, sizeof(box_t)*qty);
            box = NULL;
            free(box);
        }
    }else{
        kill("No se pudo cargar el archivo de configuracion. Vuelva a instalar el juego.");
    }

    printf("TEST BOXES\n");
    qty--;
    int i;
    for(i=0; i<qty; i++){
        printf("name: %s\n",boxList[i].name);
        printf("attack:+ %d\n",boxList[i].attack);
        printf("defense:+ %d\n",boxList[i].defense);
        printf("--------------------------\n");
    }
}
box_t getBox(int position){
    box_t box;
    return box;
}
